package com.huike.clues.service.impl;

import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.service.ISysDataService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.poi.ExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SysDataServiceImpl extends BaseController implements ISysDataService {
    @Resource
    SysDictDataMapper sysDictDataMapper;
    @Override
    public void insertDict(SysDictData sysDictData) {
        //字段填充
        String name = SecurityUtils.getUsername();
        Date date = DateUtils.getNowDate();
        sysDictData.setCreateBy(name);
        sysDictData.setCreateTime(date);
        sysDictData.setUpdateBy(name);
        sysDictData.setUpdateTime(date);
        sysDictDataMapper.insertDict(sysDictData);
    }

    @Override
    public void updateDict(SysDictData sysDictData) {
        //字段填充
        String name = SecurityUtils.getUsername();
        Date date = DateUtils.getNowDate();
        sysDictData.setUpdateBy(name);
        sysDictData.setUpdateTime(date);
        sysDictDataMapper.updateDict(sysDictData);
    }

    @Override
    public AjaxResult export() {
        startPage();
        List<SysDictData> list = sysDictDataMapper.findAll(null);
        TableDataInfo<List<SysDictData>> dataTable = getDataTable(list);
        ExcelUtil<SysDictData> excel = new ExcelUtil<>(SysDictData.class);
        return excel.exportExcel(dataTable.getRows(),"字典数据导出");
    }

    @Override
    public TableDataInfo<List<SysDictData>> list() {
        startPage();
        List<SysDictData> list = sysDictDataMapper.findAll(null);
        return getDataTable(list);

    }

    @Override
    public TableDataInfo<List<SysDictData>> listByDictType(SysDictData sysDictData) {
        List<SysDictData> list = sysDictDataMapper.findAll(sysDictData);
        return getDataTable(list);
    }

    @Override
    public void deleteByDictType(String dictType) {
        if(StringUtils.isEmpty(dictType)){
            throw  new RuntimeException("字典类型不能为空 不能删除");
        }
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictType);
        sysDictDataMapper.deleteByDictType(sysDictData);
    }

    @Override
    public TableDataInfo<List<SysDictData>> listByDictCode(String dictCode) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictCode);
        List<SysDictData> list = sysDictDataMapper.findAll(null);
        return getDataTable(list);
    }
}
